/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaPaises.java
 *
 * Created on 17/10/2011, 07:58:04
 */
package projetosustentabilidade;

import connectionBD.mySQLconsultas;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class ListaPaises extends javax.swing.JFrame {

    private ArrayList<String> listaNomeContinentes;
    private ArrayList<String> listaNomePaises;
    private mySQLconsultas consultas;

    /** Creates new form ListaPaises */
    public ListaPaises() {
        initComponents();
        consultas = new mySQLconsultas();
    }

    public ListaPaises(ArrayList<String> listaNomeContinentesParam) {
        initComponents();
        listaNomeContinentes = listaNomeContinentesParam;
        consultas = new mySQLconsultas();
        preencheListaPaises();

    }

    private void preencheListaPaises() {
        getTodosPaises();
        DefaultListModel modelListaNomePaises = new DefaultListModel();

        for (String nomePais : listaNomePaises) {
            modelListaNomePaises.addElement(nomePais);
        }

        jli_paisesi.setModel(modelListaNomePaises);
    }

    private void getTodosPaises() {
        mySQLconsultas consultasBd = new mySQLconsultas();
        listaNomePaises = new ArrayList<String>();

        for (String nomePais : listaNomeContinentes) {
           // System.out.println("nome pais q veio parametro" + nomePais);
            listaNomePaises.addAll(consultasBd.getConsultaTodosPaisesContinente(nomePais));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jli_paisesi = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        nomeContinente = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jb_avancar = new javax.swing.JButton();
        jb_voltar = new javax.swing.JButton();
        jb_exibirInformacoes = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jScrollPane1.setViewportView(jli_paisesi);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Selecione um ou mais países");

        nomeContinente.setFont(new java.awt.Font("Tahoma", 1, 14));
        nomeContinente.setText("Nome Continente");

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/projetosustentabilidade/globo_home.jpg"))); // NOI18N

        jb_avancar.setText("Avançar");
        jb_avancar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_avancarActionPerformed(evt);
            }
        });

        jb_voltar.setText("Voltar");
        jb_voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_voltarActionPerformed(evt);
            }
        });

        jb_exibirInformacoes.setText("Exibir Informações");
        jb_exibirInformacoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_exibirInformacoesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jb_exibirInformacoes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(182, Short.MAX_VALUE)
                .addComponent(jb_avancar)
                .addGap(18, 18, 18)
                .addComponent(jb_voltar)
                .addGap(188, 188, 188))
            .addGroup(layout.createSequentialGroup()
                .addGap(185, 185, 185)
                .addComponent(nomeContinente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(215, 215, 215))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nomeContinente)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jb_exibirInformacoes))
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 271, Short.MAX_VALUE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_avancar)
                    .addComponent(jb_voltar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_voltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_voltarActionPerformed
       this.dispose();
    }//GEN-LAST:event_jb_voltarActionPerformed

    private ArrayList<String> getPaisesSelecionados(){
        ArrayList<String> paisesSelecionados = new ArrayList<String>();
        Object[] nomeSelecionadosNaLista = jli_paisesi.getSelectedValues();

        for(Object o : nomeSelecionadosNaLista){
            paisesSelecionados.add((String) o);
        }
        return paisesSelecionados;
    }

    private void jb_avancarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_avancarActionPerformed
        ArrayList<String> paisesSelecionados = getPaisesSelecionados();


            if (paisesSelecionados.size() > 0) {
                ListaAtributos telaListaAtributos = new ListaAtributos(paisesSelecionados);
                telaListaAtributos.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, "Você deve selecionar pelo menos um pais antes de avançar");
            }




    }//GEN-LAST:event_jb_avancarActionPerformed

    private void jb_exibirInformacoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_exibirInformacoesActionPerformed
        ArrayList<String> paisesSelecionados = getPaisesSelecionados();
        

        if (paisesSelecionados.size() > 0) {

            for (String pais : paisesSelecionados){               
                InfoTexto infoTexto = new InfoTexto(pais ,consultas.getInfoPais(pais));
                infoTexto.setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Você deve selecionar pelo menos um pais para exibir informações sobre ele");
        }

        consultas.close();
    }//GEN-LAST:event_jb_exibirInformacoesActionPerformed

    @Override
    public void dispose() {
        consultas.close();
        super.dispose();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jb_avancar;
    private javax.swing.JButton jb_exibirInformacoes;
    private javax.swing.JButton jb_voltar;
    private javax.swing.JList jli_paisesi;
    private javax.swing.JLabel nomeContinente;
    // End of variables declaration//GEN-END:variables

}
